package com.framework.loippi.controller.admin;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.Ad;
import com.framework.loippi.mybatis.paginator.domain.Order.Direction;
import com.framework.loippi.service.AdService;
import com.framework.loippi.service.UserService;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.Paramap;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Controller - 广告
 *
 * @author Mounate Yan。
 * @version 1.0
 */
@Controller("adminAdController")
@RequestMapping({"/admin/ad"})
public class AdController extends GenericController {

    @Resource
    private AdService adService;

    @Resource
    private UserService userService;


    /**
     * 跳转添加页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = {"/add"}, method = {RequestMethod.GET})
    public String add(ModelMap model, Pageable pageable, HttpServletRequest request) {
        return "/admin/ad/add";
    }

    /**
     * 用于获取模态框中的数据
     *
     * @param model
     * @param pageable
     * @param request
     * @return
     */
    @RequestMapping(value = {"/findProductListOfModel"}, method = {RequestMethod.POST})
    @ResponseBody
    public String findProductListOfModel(ModelMap model, Pageable pageable, HttpServletRequest request) {
        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        paramter.put("status", 1);
        pageable.setParameter(paramter);
        pageable.setPageSize(10);
        return ApiUtils.success();
    }

//	/**
//	 * 保存
//	 */
//	@RequestMapping(value = "/save", method = RequestMethod.POST)
//	public String save(Ad ad, RedirectAttributes redirectAttributes) {
//		/*if (ad.getStartDate() != null && ad.getEndDate() != null && ad.getStartDate().after(ad.getEndDate())) {
//			return ERROR_VIEW;
//		}*/
//
//		ad.setCreator(userService.getCurrent().getId());
//		ad.setCreateDate(new Date());
//		if (ad.getEndDate().compareTo(new Date()) < 0) {
//			ad.setStatus(3);
//		}
//
//		if (ad.getStartDate().compareTo(new Date()) < 0 && ad.getEndDate().compareTo(new Date()) > 0) {
//			ad.setStatus(1);
//		}
//
//		if (ad.getStartDate().compareTo(new Date()) > 0) {
//			ad.setStatus(2);
//		}
//		adService.save(ad);
//		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
//		return "redirect:list.jhtml";
//	}


    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Message saveAd(Ad ad, RedirectAttributes redirectAttributes) {
		/*if (ad.getStartDate() != null && ad.getEndDate() != null && ad.getStartDate().after(ad.getEndDate())) {
			return ERROR_VIEW;
		}*/
        Map<String, Object> map = new HashMap<>();
        if (null != ad.getStartDate() && null != ad.getEndDate()) {
            Integer type = ad.getType();
            String position = ad.getPosition();
            if (4 == type && !"1".equals(position)) {
                Ad ad1 = adService.find(Paramap.create().put("status", 1).put("position", position));
                if (null != ad1) {
                    if (ad.getEndDate().compareTo(ad.getStartDate()) < 0) {
                        return Message.error("结束时间不能小于开始时间");
                    }
                    if (ad.getStartDate().compareTo(ad1.getEndDate()) < 0) {
                        return Message.warn("所选广告位已有进行中的");
                    }

                }
            }
        }

        ad.setCreator(userService.getCurrent().getId());
        ad.setCreateDate(new Date());
        if (ad.getEndDate().compareTo(new Date()) < 0) {
            ad.setStatus(3);
        }

        if (ad.getStartDate().compareTo(new Date()) < 0 && ad.getEndDate().compareTo(new Date()) > 0) {
            ad.setStatus(1);
        }

        if (ad.getStartDate().compareTo(new Date()) > 0) {
            ad.setStatus(2);
        }
        adService.save(ad);
//		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return SUCCESS_MESSAGE;
    }


    /**
     * 编辑
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Long id, ModelMap model) {
        Ad ad = adService.find(id);
        return "/admin/ad/edit";
    }

    /*	*//**
     * 更新
     *//*
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Ad ad, RedirectAttributes redirectAttributes) {
		*//*if (ad.getStartDate() != null && ad.getEndDate() != null && ad.getStartDate().after(ad.getEndDate())) {
			return ERROR_VIEW;
		}*//*
		if (ad.getEndDate().compareTo(new Date()) < 0) {
			ad.setStatus(3);
		}

		if (ad.getStartDate().compareTo(new Date()) < 0 && ad.getEndDate().compareTo(new Date()) > 0) {
			ad.setStatus(1);
		}

		if (ad.getStartDate().compareTo(new Date()) > 0) {
			ad.setStatus(2);
		}
		ad.setCreator(userService.getCurrent().getId());
		ad.setCreateDate(new Date());
		adService.update(ad);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}*/

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Message update(Ad ad, RedirectAttributes redirectAttributes) {
		/*if (ad.getStartDate() != null && ad.getEndDate() != null && ad.getStartDate().after(ad.getEndDate())) {
			return ERROR_VIEW;
		}*/

        if (!"1".equals(ad.getPosition()) && null != ad.getStartDate() && null != ad.getEndDate()) {
            Integer type = ad.getType();
            String position = ad.getPosition();
            if (4 == type && !"1".equals(position)) {
                Ad ad1 = adService.find(Paramap.create().put("status", 1).put("position", position));
                if (null != ad1 && !ad.getId().equals(ad1.getId())) {
                    if (ad.getEndDate().compareTo(ad.getStartDate()) < 0) {
                        return Message.error("结束时间不能小于开始时间");
                    }
                    if (ad.getStartDate().compareTo(ad1.getEndDate()) < 0) {
                        return Message.warn("所选广告位已有进行中的");
                    }
                }

            }
        }
        if (ad.getEndDate().compareTo(new Date()) < 0) {
            ad.setStatus(3);
        }

        if (ad.getStartDate().compareTo(new Date()) < 0 && ad.getEndDate().compareTo(new Date()) > 0) {
            ad.setStatus(1);
        }

        if (ad.getStartDate().compareTo(new Date()) > 0) {
            ad.setStatus(2);
        }
        ad.setCreator(userService.getCurrent().getId());
        ad.setCreateDate(new Date());
        adService.update(ad);
//		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return SUCCESS_MESSAGE;
    }

    /**
     * 列表查询
     *
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    public String list(Pageable pageable, ModelMap model, HttpServletRequest request, String orderProperty, Integer sort) {
        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        Map map = new HashMap();
        for (String key : paramter.keySet()) {
            if (!StringUtil.isEmpty(paramter.get(key).toString())) {
                map.put(key, paramter.get(key));
            }
        }
        pageable.setParameter(map);

        //排序
        if (null != orderProperty && !orderProperty.equals("") && sort != null) {
            if (sort == 0) {
                pageable.setOrderDirection(Direction.ASC);
            } else {
                pageable.setOrderDirection(Direction.DESC);
            }
            pageable.setOrderProperty(orderProperty);
        } else {
            pageable.setOrderDirection(Direction.DESC);
            pageable.setOrderProperty("START_DATE");
        }
        paramter.put("sort", sort);
        paramter.put("orderProperty", orderProperty);

        model.addAttribute("paramter", paramter);
        model.addAttribute("page", this.adService.findByPage(pageable));
        return "/admin/ad/list";
    }

    /**
     * 删除操作
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    @ResponseBody
    public Message delete(Long[] ids) {
        this.adService.deleteAll(ids);
        return SUCCESS_MESSAGE;
    }


}
